package com.germinus.cardea;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;

import com.germinus.cardea.bbdd.Localization;
import com.germinus.cardea.bbdd.Medicine;
import com.germinus.cardea.bbdd.MedicineManager;
import com.germinus.cardea.bbdd.MedicineType;
import com.germinus.cardea.bbdd.MedicineTypeEvent;
import com.germinus.cardea.bbdd.StockEvent;
import com.germinus.cardea.bbdd.StockTypeEvent;
import com.germinus.cardea.util.HibernateUtil;

public class CardeaManager extends HttpServlet {

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		SimpleDateFormat dateFormatter = new SimpleDateFormat(
				"yyyy-MM-dd");
		MedicineManager medicineManager = new MedicineManager();
		Long eType = Long.valueOf(request.getParameter("type"));

		List<MedicineTypeEvent> eventTypeList = medicineManager
				.listMedicineEventType(eType);
		List<MedicineType> types = medicineManager.listMedicineType(request
				.getParameter("name"));
		List<Localization> locatizations = medicineManager
				.listLocalization(request.getParameter("localization"));

		try {

			if (types != null && eventTypeList != null && locatizations != null) {
				MedicineType mtype = types.get(0);
				MedicineTypeEvent meTypeEvent = eventTypeList.get(0);
				Localization meLocalization = locatizations.get(0);
				MedicineTypeEvent eventType = new MedicineTypeEvent();
				Date expDate = dateFormatter.parse(request
						.getParameter("expDate"));
				Date tagDate = dateFormatter.parse(request
						.getParameter("tagDate"));
				Medicine med = medicineManager.createAndStoreMedicine(request
						.getParameter("tagId"), tagDate, expDate,
						meLocalization, mtype);
				medicineManager.createandStoreMedEvent(med, meTypeEvent,
						request.getParameter("message"));
				String stock = request.getParameter("stock");
				System.out.println("mtype.id " + mtype.getId());
				if (stock != null && stock != "") {
					int stockValue = Integer.parseInt(stock);
					List<StockEvent> stockEventsList = medicineManager.listStockEventToday(mtype);
					List<StockTypeEvent> stockTypeEventsList = medicineManager
							.listStockEventType(Long.parseLong("1"));
					if (stockEventsList.size() >0) {
						StockEvent stockEvent = stockEventsList.get(0);

						System.out.println("Stock anterior: "
								+ stockEvent.getStock() + "  nuevo stock:"
								+ stockValue);
					} else {
					System.out.println("CARDEA: ARRAY "+stockEventsList.size());
						Date now = new Date();
						medicineManager.createAndStoreStockEvent(mtype, now,
								stockTypeEventsList.get(0), "", stockValue);
					}
				}
			}

		} catch (Exception ex) {
			HibernateUtil.getSessionFactory().getCurrentSession()
					.getTransaction().rollback();
			throw new ServletException(ex);
		}

	}

	private void printEventForm(PrintWriter out) {
		out.println("<h2>Add new event:</h2>");
		out.println("<form>");
		out.println("Title: <input name='eventTitle' length='50'/><br/>");
		out
				.println("Date (e.g. 24.12.2009): <input name='eventDate' length='10'/><br/>");
		out.println("<input type='submit' name='action' value='store'/>");
		out.println("</form>");
	}

	private void listEvents(PrintWriter out, SimpleDateFormat dateFormatter) {

		/*
		 * List result = HibernateUtil.getSessionFactory()
		 * .getCurrentSession().createCriteria(Event.class).list(); if
		 * (result.size() > 0) { out.println("<h2>Events in database:</h2>");
		 * out.println("<table border='1'>"); out.println("<tr>");
		 * out.println("<th>Event title</th>");
		 * out.println("<th>Event date</th>"); out.println("</tr>"); for
		 * (Iterator it = result.iterator(); it.hasNext();) { // Event event =
		 * (Event) it.next(); out.println("<tr>"); out.println("<td>" +
		 * event.getTitle() + "</td>"); out.println("<td>" +
		 * dateFormatter.format(event.getDate()) + "</td>");
		 * out.println("</tr>"); } out.println("</table>"); }
		 */
	}

	protected void createAndStoreEvent(String title, Date theDate) {
		// Event theEvent = new Event();
		// theEvent.setTitle(title);
		// theEvent.setDate(theDate);

		// HibernateUtil.getSessionFactory()
		// .getCurrentSession().save(theEvent);
	}

}
